﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{    
    public partial class MainForm
    {
        Rota RotaSelectedInCalendar
        {
            get
            {
                if ((sheetCalendar.DataSource as BindingList<Rota>) == null
                    || sheetCalendar.SelectedRows.Count == 0)
                {
                    return null;
                }
                
                return sheetCalendar.SelectedRows[0].DataBoundItem as Rota;
            }
        }

        static bool IsJumpPossible(RelativePosition pWhere)
        {
            var rota = Program.CurrentFile.GetNeighbouringRota(
                Program.CurrentFile.CurrentRota,
                pWhere);
            if (rota == null)
                return false;
            return rota.ProperAction != RotaAction.None;
        }

        void SetEnabledForJumpingButtons()
        {
            btnCurrentWeek.Enabled = IsJumpPossible(RelativePosition.This);
            btnNextWeek.Enabled = IsJumpPossible(RelativePosition.Next);            
        }

        void AdjustcbWeekValue()
        {
            if (CurrentRota == null)
            {
                return;
            }
            cbWeek.Tag = true;
            var _index = (cbWeek.DataSource as IList<KeyValuePair<string, DateTime>>).
                TakeWhile(_item => _item.Value != CurrentRota.Monday).Count();
            cbWeek.SelectedIndex = _index;
            cbWeek.Tag = false;
        }

        void PerformAction(Rota pRota, RotaAction pAction)
        {
            if (CurrentRota != null &&
                !CurrentRota.OKLeaving())
            {
                return;
            }

            #region action CREATE
            if (pAction == RotaAction.Create)
            {
                var _chosen_template = Template.DefaultName;                
                if (Program.CurrentFile.AnyChoiceOfTemplates)
                {
                    Program.frmChooseBaseTemplate = new ChooseTemplate(
                        Resources.FORM_CHOOSETEMPLATE_GroupBoxCaption_CreateRota.Replace(
                        "#",
                        pRota.bndDays),
                        false);
                    if (Program.frmChooseBaseTemplate.ShowDialog() == DialogResult.OK)
                    {
                        _chosen_template = Program.frmChooseBaseTemplate.GetChoice;
                    }
                    else
                    {
                        return;
                    }
                }
                Sounds.Play(Sounds.Pageturn);
                SwitchToTab(tpRota);
                LoadRota(
                    Program.CurrentFile.ActivateEmptyAndPass(
                    pRota.Monday,
                    _chosen_template));                
                AdjustSheetTempRotaHeight();
            }
            #endregion

            #region action EDIT / VIEW

            if (pAction == RotaAction.Edit |
                pAction == RotaAction.View)
            {
                Hourglass(true);                
                Sounds.Play(Sounds.Pageturn);
                SwitchToTab(tpRota);
                LoadRota(Program.CurrentFile.GetRotaCopy(pRota.Monday));
                AdjustSheetTempRotaHeight();
                Hourglass(false);
            }
            #endregion
        }
    }
}
